﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKHO.Class;

namespace QLKHO.Forms
{
    public partial class frmChiTietPhieuNhap : Form
    {
        private string strMSHangHoa = "";
        private QLKHODataSet.CHITIETPHIEUNHAPDataTable dtPhieuNhap = new QLKHODataSet.CHITIETPHIEUNHAPDataTable();
        public frmChiTietPhieuNhap()
        {
            InitializeComponent();
            this.cHITIETPHIEUNHAPTableAdapter.Fill(this.qLKHODataSet.CHITIETPHIEUNHAP);
            clsMain.flexConn.InitForms(this.Tag.ToString(), this, qLKHODataSet.CHITIETPHIEUNHAP);
            clsMain.flexConn.InitForms(this.Tag.ToString(), ref fcButton1);
            clsMain.flexConn.InitForms(this.Tag.ToString(), ref dgvChiTietPhieuNhapKho);
            clsMain.flexConn.KhoiTaoXMLTable(this.Tag.ToString(), qLKHODataSet.CHITIETPHIEUNHAP);
        }

        private void frmChiTietPhieuNhap_Load(object sender, EventArgs e)
        {
            this.hANGHOATableAdapter.Fill(this.qLKHODataSet.HANGHOA);
            txtMSPhieuNhap.fcText = clsMain.strMSPhieuNhapKho;
            clsMain.flexConn.GenerateCode(13);
            LoadData();
        }
        private void LoadData()
        {
            try
            {
                dtPhieuNhap.Clear();
                foreach (DataRow row in qLKHODataSet.CHITIETPHIEUNHAP)
                {
                    if (row["MSPHIEUNHAP"].ToString() == clsMain.strMSPhieuNhapKho)
                    {
                        DataRow rowPhieuNhap = dtPhieuNhap.NewRow();
                        for (int i = 0; i < qLKHODataSet.CHITIETPHIEUNHAP.Columns.Count; i++)
                        {
                            rowPhieuNhap[i] = row[i];

                        }
                        dtPhieuNhap.Rows.Add(rowPhieuNhap);
                    }
                }
            cHITIETPHIEUNHAPBindingSource1.DataSource = dtPhieuNhap;

            clsMain.intThanhTien = 0;
                int j = 0;
                foreach (DataRow row in dtPhieuNhap.Rows)
                {
                    string ThanhTien = Convert.ToString(Convert.ToInt32(row["SOLUONGTHUC"].ToString()) * Convert.ToInt32(row["DONGIA"].ToString()));
                    dgvChiTietPhieuNhapKho.Rows[j].Cells["THANHTIEN"].Value = ThanhTien;
                    j++;
                    clsMain.intThanhTien += Convert.ToInt32(ThanhTien);
                }

            txtThanhTien.Text = clsMain.intThanhTien.ToString("c0");
            }
            catch (Exception ex)
            {
                MessageBox.Show("LoadData\n"+ex.ToString());
            }
            
        }
        private void dgvChiTietPhieuNhapKho_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = 0;
            foreach (DataRow row in qLKHODataSet.CHITIETPHIEUNHAP)
            {
                if (row["MASO"].ToString() == dtPhieuNhap.Rows[e.RowIndex]["MASO"])
                {
                    break;
                }
                rowIndex++;
            }
            clsMain.flexConn.AddDataToControls(this.Tag.ToString(), rowIndex, qLKHODataSet.CHITIETPHIEUNHAP);
        }

        private void fcButton1_Click(object sender, EventArgs e)
        {
            if (fcButton1.intState == FlexControls.flexControls.STATE_DELETE)
            {
                int RowHandle=clsMain.flexConn.Forms_QLHS.fcForms[13].intRowHandle;
                string MaSo=qLKHODataSet.CHITIETPHIEUNHAP.Rows[RowHandle]["MASO"].ToString();
                string MSHANGHOA=qLKHODataSet.CHITIETPHIEUNHAP.Rows[RowHandle]["MSHANGHOA"].ToString();
                int SOLUONGCHUNGTU=Convert.ToInt32( qLKHODataSet.CHITIETPHIEUNHAP.Rows[RowHandle]["SOLUONGCHUNGTU"].ToString());
                int SOLUONGTHUC =Convert.ToInt32( qLKHODataSet.CHITIETPHIEUNHAP.Rows[RowHandle]["SOLUONGTHUC"].ToString());
                int DONGIA = Convert.ToInt32(qLKHODataSet.CHITIETPHIEUNHAP.Rows[RowHandle]["DONGIA"].ToString());
                cHITIETPHIEUNHAPTableAdapter.Delete(MaSo, clsMain.strMSPhieuNhapKho, MSHANGHOA, SOLUONGCHUNGTU, SOLUONGTHUC, DONGIA);
                this.cHITIETPHIEUNHAPTableAdapter.Fill(this.qLKHODataSet.CHITIETPHIEUNHAP);
            }
            else
            clsMain.flexConn.ButtonsClick(this.Tag.ToString(), fcButton1.intState);
            
            cHITIETPHIEUNHAPTableAdapter.Update(qLKHODataSet.CHITIETPHIEUNHAP);
            txtMSPhieuNhap.fcText = clsMain.strMSPhieuNhapKho;
            LoadData();
            
        }
        private void UpdateTonKho()
        {
            try
            {
                int i = 0;
                DateTime dateTonKho;
                dateTonKho = Convert.ToDateTime(qLKHODataSet.TONKHO.Rows[0]["NGAYCAPNHAT"]);
                foreach (DataRow rowTonKho in qLKHODataSet.TONKHO)
                {
                    if (dateTonKho < Convert.ToDateTime(rowTonKho["NGAYCAPNHAT"]))
                    {
                        i++;
                    }
                    
                }
            }
            catch (Exception ex)
            { }
        }
        
    }
}
